<?php
session_start();
//在页首先要开启session,

define( 'DS' , DIRECTORY_SEPARATOR );
// define( 'APP_ROOT' , dirname( dirname( __FILE__ ) ) . DS  );
define( 'APP_ROOT' , dirname( __FILE__ ) . DS  );

date_default_timezone_set("Asia/Shanghai");
$action = empty( $_REQUEST['ajax'] ) ? '' : strtolower( $_REQUEST['ajax'] );
if($action){
	if($action=='js'){
		$ContentType ='application/x-javascript';
	}else{
		$ContentType ='text/plain';
	}
}else{
	$ContentType ='text/html';
}
header("Content-Type:".$ContentType.";charset=utf-8");

// define( 'KIS_APP_ROOT' , dirname( APP_ROOT ) . DS );
define( 'KIS_APP_ROOT' , APP_ROOT . DS .'php'.DS);
// include_once KIS_APP_ROOT.'ext_lib'. DS.'comm.function.php';
define( 'AROOT' , KIS_APP_ROOT);
define( 'CROOT' , KIS_APP_ROOT.'_lp'. DS.'core'. DS  );
include_once CROOT. 'lib'.DS.'core.function.php';
// include_once( KIS_APP_ROOT. 'ext_lib'.DS .'mail.config.inc.php' );
include_once KIS_APP_ROOT.'lib'. DS.'app.function.php';

include_once 'php/map.inc.php';
include_once 'php/func.inc.php';

define('TBS_ROOT', KIS_APP_ROOT.'_lp'.DS );
define('TPLROOT', dirname( __FILE__ ) . DS. 'html'.DS );

define('APP_ENVIRONMENT', 'DEVELOPMENT');
// define('APP_ENVIRONMENT', 'PRODUCT');
// define('KIS_DEBUG_MODE', 'DISABLE_DEBUG');

/**

*/

// $db_table_prefix = 'shdic_sc2015_'; // in func.inc.php


/**



*/


$pagestartime=microtime(); 
$time_log='';

$c='guest';
$a='index';
$a='demo-frame';
$html_tpl='';
$outVar=array();

$tt = v('c') ;  if($tt) $c=$tt;
$tt = v('a') ;  if($tt) $a=$tt;

if(strlen($c)>=3 && strlen($a)>=3){
    //base check
}else{
    die('403 Access Denied/Forbidden');
}

$debugMode = 0;
$outVar['debugMode']=0;

$tt = intval(v('debug'))  ;
if ($tt>0 ){
    $debugMode=1;
    $outVar['debugMode']=1;
}

$map_uuid = null;
$map_type = null;
$grids = null;
$limit = 0;
$offset = 15;

$tt = intval(v('start'))  ;
if ($tt>0 ){
    $limit=$tt;
}
$tt = intval(v('limit'))  ;
if ($tt>0 && $tt<=500){
    $offset=$tt;
}

// init_db_info($db_table_prefix);

$user_key=null;

$html_tpl='demo_tpl';
$html_data='';

switch ($c) {
	case 'test':
        switch ($a) {
            case 'frame':
                $html_tpl='main_frame_tpl';
                $outVar['page_title'] = 'main_frame';
				$outVar['page_desc'] = 'page_desc';
				$html_data = '';
                break;

            case 'render':
                $outVar['page_title'] = 'main_frame';
				$outVar['page_desc'] = 'page_desc';
				$outVar['html_data'] = 'html_data is nothing';
				echo render( $outVar , 'web' , 'frame');
				exit;
                break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

	case 'guest':
		switch ($a) {
		// case 'index':
		// 	$html_tpl='index_tpl';
		// 	$outVar['page_title'] = 'page_title';
		// 	$outVar['page_desc'] = 'page_desc';
		// 	$html_data = '';
		// 	break;

		case 'homepage123':
			$html_tpl='homepage_tpl';
            $outVar['page_title'] = '首页';
			$outVar['page_desc'] = '首页';
			$outVar['html_res_root'] = 'html/res/homepage/';				
			$html_data = '';
			break;

		 // case 'daxue':
   //          $outVar['page_title'] = '大学';
			// $outVar['page_desc'] = 'page_desc';
			// $outVar['html_data'] = 'html_data is nothing';
			// $outVar['html_res_root'] = 'html/res/homepage/';	
			// $outVar['page_nav'] = '<a href="index.php">首页</a> > <a href="#">学校</a> >清华大学';
			// $outVar['outVar']=$outVar;
			// echo render( $outVar , 'part' , 'head');
			// echo render( $outVar , 'web' , 'daxue');
			// echo render( $outVar , 'part' , 'foot');
			// exit;

		// case 'dingwei':
  //           $outVar['page_title'] = '大学dingwei';
		// 	$outVar['page_desc'] = 'page_desc';
		// 	$outVar['html_data'] = 'html_data is nothing';
		// 	$outVar['html_res_root'] = 'html/res/homepage/';	
		// 	$outVar['page_nav'] = '<a href="index.php">首页</a> > <a href="#">学校</a> >清华大学';
		// 	$outVar['outVar']=$outVar;

		// 	$src = render( $outVar , 'web' , 'dingwei');
		// 	$tt = render( $outVar , 'part' , 'headnav');
		// 	$ttf = render( $outVar , 'part' , 'foot');

		// 	$src =str_replace('<!-- $headnav$ -->', $tt, $src);
		// 	echo str_replace('<!-- $foot$ -->', $ttf, $src);			
		// 	exit;

		default:
			// die('403 Access Denied/Forbidden '.$c);
			$outVar['page_title'] = 'Miss';
			$outVar['page_desc'] = 'page_desc';
			$outVar['html_data'] = 'html_data is nothing';
			$outVar['html_res_root'] = 'html/res/homepage/';	
			$outVar['page_nav'] = '<a href="index.php">首页</a> ';
			$outVar['urlparms']='c='.$c.'&a='.$a;

			$outVar['outVar']=$outVar;

			$quick_exit=false;
			$tt = 'html/web/' . $a . '.tpl.html';
			if(file_exists($tt)){
				$src = render( $outVar , 'web' , $a);
				
			}else{
				$quick_exit=true;
				$src = render( $outVar , 'part' , 'notready');
			}

			$tt = render( $outVar , 'part' , 'headnav');
			$ttf = render( $outVar , 'part' , 'foot');
			$src =str_replace('<!-- $headnav$ -->', $tt, $src);
			$src =str_replace('<!-- $foot$ -->', $ttf, $src);

			$part_delimiter='<!-- $';
			$part_end_delimiter='$ -->';
			if(!$quick_exit && stripos($src, $part_delimiter)!==false){
				$tmparr=explode($part_delimiter, $src);
				foreach ($tmparr as $tmparrkey => $tmparrvalue) {
					if(stripos($tmparrvalue, $part_end_delimiter)!==false){
						$tmp_key = explode($part_end_delimiter, $tmparrvalue)[0];
						$tmp_key = trim($tmp_key);
						if(!empty($tmp_key)){
							$tmp_key_file = 'html/part/' . $tmp_key . '.tpl.html';
							if(file_exists($tmp_key_file)){						
								$tmp_key_src = render( $outVar , 'part' , $tmp_key);
							}else{
								$tmp_key_src=" miss ".$tmp_key;
							}	
							$src =str_replace('<!-- $'.$tmp_key.'$ -->', $tmp_key_src, $src);
						}
					}else{
						//skip
					}
				}
			}
			echo $src;
			exit;
		}
		break;

	case 'career':
        switch ($a) {
            case 'home':
                // $html_tpl='career_home_tpl';
                $outVar['page_title'] = 'main_frame';
				$outVar['page_desc'] = 'page_desc';
				$outVar['html_data'] = 'not ready!!!';
				echo render( $outVar , 'web' , 'frame');
				exit;

                break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

    case 'api':
        switch ($a) {
            case 'itmcatjs':
                // $html_data = get_item_cat_js();
                break;

            default:
                die('403 Access Denied/Forbidden '.$c);
        }
        break;

	case 'user':
		switch ($a) {
		case 'index':
			$html_tpl='list_lootset_tpl';
			$outVar['url_para'] = '&pid=0';
			$html_data = '';
			break;

		default:
			die('403 Access Denied/Forbidden '.$c);
		}
		break;
	
	default:
		die('403 Access Denied/Forbidden');
}

/**


*/

if($c=='api' && $action){
    echo $html_data;

}else if($a){

    $time_log.= "页面运行时间: ". used_time($pagestartime) ."秒.<br/>".PHP_EOL; 

	$out = array(
		'ver' => 'v20151110', 
        'app_version' => 'v20151110', 
		'html_res_root' => 'html/res2', 
		'res_root' => 'html/res', 

		'user_key' => $user_key==null?'new':$user_key,
        'data'=> $html_data,

        'time_log'=> $time_log,
        'page_count'=>$offset,

        'js_css_ver'=>'20151110',

        'last_modify_time'=>date('Y-m-d H:i:s'),
		'author' => '492607291#qq.com'
	);
	
    foreach ($outVar as $key => $value) {
        $out[$key] = $value;
    }
	/* Load and display map */
    if(empty($html_tpl)){
        $html_tpl='base_tpl';
    }
	$tbs = template_load($html_tpl);
	$tbs->Show();
}

?>

